package com.baibei.order.emptyview;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.baibei.order.R;

/**
 * 空界面adapter
 * Created by shenminjie on 2017/8/11.
 * mail:shenminjie@100bei.com
 */

public abstract class EmptyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    /**
     * 空视图view
     */
    public static final int VIEWTYPE_EMPTYVIEW = Integer.MAX_VALUE - 1;

    /**
     * 是否开启emptyview
     */
    private boolean isOpenEmptyView;

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEWTYPE_EMPTYVIEW) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_empty_view, parent, false);
            return new EmptyViewHolder(view);
        }
        return onCreateItemViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof EmptyViewHolder) {
            EmptyViewHolder viewHolder = (EmptyViewHolder) holder;
            EmptyResourceInfo info = getEmptyResourceInfo();
            if (info != null) {
                TextView textView = viewHolder.getEmptyTextView();
                textView.setText(info.getCharSequence());
                textView.setCompoundDrawablesWithIntrinsicBounds(0, info.getResource(), 0, 0);
                textView.setOnClickListener(info.getOnClickListener());
                onBindEmptyViewHolder(viewHolder);
            }
        }
        onBindItemViewHolder(holder, position);
    }

    @Override
    public int getItemCount() {
        if (isOpenEmptyView && getCount() == 0) {
            return 1;
        }
        return getCount();
    }

    @Override
    public int getItemViewType(int position) {
        if (getCount() == 0) {
            return VIEWTYPE_EMPTYVIEW;
        }
        return super.getItemViewType(position);
    }

    /**
     * 获取视图
     *
     * @return 数量
     */
    public abstract int getCount();

    /**
     * 构建viewholder
     *
     * @param parent   parent
     * @param viewType viewType
     */
    public abstract RecyclerView.ViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType);

    /**
     * 处理viewHolder
     *
     * @param holder   holder
     * @param position position
     */
    public abstract void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position);


    /**
     * 是否开启空视图模式
     *
     * @param isOpen isOpen
     */
    public void isOpenEmptyView(boolean isOpen) {
        isOpenEmptyView = isOpen;
    }


    /**
     * 获取空视图资源配置
     *
     * @return return
     */
    public EmptyResourceInfo getEmptyResourceInfo() {
        return new EmptyResourceInfo(R.mipmap.icon_empty, "啥也没有呢~");
    }

    /**
     * 交给下一层处理textview
     *
     * @param holder holder
     */
    public void onBindEmptyViewHolder(EmptyViewHolder holder) {
        //nothing
    }

}
